package org.ccahouse.ccatrain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ccahouse.ccacommons.common.PageResult;
import org.ccahouse.ccacommons.common.RestResult;
import org.ccahouse.ccacommons.common.ResultUtils;
import org.ccahouse.ccatrain.dto.HospitalDto;
import org.ccahouse.ccatrain.dto.HospitalListPageDto;
import org.ccahouse.ccatrain.entity.Hospital;
import org.ccahouse.ccatrain.mapper.HospitalMapper;
import org.ccahouse.ccatrain.mapper.UserMapper;
import org.ccahouse.ccatrain.service.IHospitalService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, Hospital> implements IHospitalService {

    @Override
    public RestResult getHospitalList() {
        List<Hospital> hospitals = this.baseMapper.selectList(new QueryWrapper<Hospital>().
                lambda().eq(Hospital::getCity, "深圳"));
        return ResultUtils.success(hospitals);
    }

    @Override
    public RestResult getHospitalPage(HospitalListPageDto hospitalListPageDto) {
        PageResult<Hospital> hospitals = hospitalListPageDto.startPage();

        LambdaQueryWrapper<Hospital> queryWrapper = new QueryWrapper<Hospital>().lambda();
        if(hospitalListPageDto.getProvince() != null){
            queryWrapper.eq(Hospital::getProvince, hospitalListPageDto.getProvince());
        }

//        this.baseMapper.selectList(new QueryWrapper<Hospital>().lambda()
//            .eq(Hospital::getProvince, hospitalListPageDto.getProvince())
//        );
        this.baseMapper.selectList(queryWrapper);

        return ResultUtils.successPage(hospitals.getTotal(), hospitals.getResult());
    }

    @Override
    public RestResult saveHospital(Hospital hospital) {
        this.save(hospital);
        return ResultUtils.success();
    }

    @Override
    public RestResult saveHospitals(List<Hospital> hospitals) {
        this.saveBatch(hospitals);
        return ResultUtils.success();
    }

    @Override
    public RestResult updateHospital(HospitalDto hospital) {
        this.update(new UpdateWrapper<Hospital>().lambda()
                .set(Hospital::getHosName, hospital.getHosName())
                .eq(Hospital::getId, hospital.getId())
        );
        return ResultUtils.success();
    }

    @Override
    public RestResult getOneById(Integer id) {
        Hospital hospital = this.baseMapper.getOneById(id);
        return ResultUtils.success(hospital);
    }

    @Override
    public RestResult getDetailById(Integer id) {
        Hospital hospital = this.baseMapper.getDetailById(id);
        return ResultUtils.success(hospital);    }

    @Override
    public RestResult getHospitalByIds(List<Integer> ids) {
        List<Hospital> hospitals = this.baseMapper.getHospitalByIds(ids);
        return ResultUtils.success(hospitals);
    }

}
